package com.epalmpay.controller.channel.mall;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.epalmpay.commom.BaseController;
import com.epalmpay.dto.system.ShopListOutDTO;
import com.epalmpay.dto.system.ShopSelectQueryDTO;
import com.epalmpay.service.shop.IShopService;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.mall.*;
import com.epalmpay.entity.MallGood;
import com.epalmpay.entity.Page;
import com.epalmpay.service.mall.IMallGoodService;
import com.epalmpay.util.PropertyCopyUtil;
import com.epalmpay.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.epalmpay.dto.mall.MallGoodInDTO;
import com.epalmpay.dto.mall.MallGoodListOutDTO;
import com.epalmpay.dto.mall.MallGoodOutDTO;
import com.epalmpay.dto.mall.MallGoodQueryDTO;
import com.epalmpay.dto.mall.MallGoodSelectiveListOutDTO;
import com.epalmpay.dto.mall.MallGoodSelectiveQueryDTO;
import com.epalmpay.dto.mall.MallGoodUpdateDTO;

/**
 * Created by lifayi on 2017/5/20.
 */
@RestController
@RequestMapping("/merchant/mall")
public class MallGoodController extends BaseController {

    @Autowired
    private IMallGoodService mallGoodService;

    @Autowired
    private IShopService shopService;

    /*
    * 新增和修改商品
    * */
    @RequestMapping("/addMallGood")
    public BaseOutDTO addMallGood(@RequestBody MallGoodInDTO mallGoodInDTO) {
        try {
            return mallGoodService.addMallGood(mallGoodInDTO);
        } catch (Exception e) {
            e.printStackTrace();
            BaseOutDTO baseOutDTO = new BaseOutDTO();
            baseOutDTO.setErrorCode(Rescode.FAIL);
            return baseOutDTO;
        }
    }

    /*
    * 商品列表
    * */
    @RequestMapping("/listMallGood")
    public MallGoodListOutDTO listMallGood(MallGoodQueryDTO mallGoodQueryDTO, PageDTO pageDTO) {
        MallGoodListOutDTO mallGoodListOutDTO = new MallGoodListOutDTO();
        List<MallGoodOutDTO> mallGoodOutDTOList = new ArrayList<MallGoodOutDTO>();

        Page page = Tools.pageEmtity(pageDTO);

        List<MallGood> mallGoodList = null;

        try {
            mallGoodList = mallGoodService.listMallGood(mallGoodQueryDTO, page);
            for(int i = 0; i < mallGoodList.size(); i++) {
                MallGoodOutDTO mallGoodOutDTO = new MallGoodOutDTO();
                PropertyCopyUtil.copyProperties(mallGoodList.get(i), mallGoodOutDTO);
                mallGoodOutDTO.setCanUsedShopCount(mallGoodList.get(i).getCanUsedShop().split(",").length);
                mallGoodOutDTO.setPicture(mallGoodOutDTO.getPicture().split(",")[0]);   //只要第一张图片的url
                mallGoodOutDTOList.add(mallGoodOutDTO);
            }
            mallGoodListOutDTO.setMallGoodOutDTOList(mallGoodOutDTOList);
            mallGoodListOutDTO.setPage(page);
            mallGoodListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
        } catch (ParseException e) {
            logger.error("user parameter parse error");
            e.printStackTrace();
        } catch (Exception e) {
            mallGoodListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "服务器处理异常");
            e.printStackTrace();
        }
        return mallGoodListOutDTO;
    }

    /*
    * 代理商商品列表
    * */
    @RequestMapping("/listAgentMallGood")
    public MallGoodListOutDTO listAgentMallGood(MallGoodQueryDTO mallGoodQueryDTO, PageDTO pageDTO) {
        MallGoodListOutDTO mallGoodListOutDTO = new MallGoodListOutDTO();
        List<MallGoodOutDTO> mallGoodOutDTOList = new ArrayList<MallGoodOutDTO>();

        Page page = Tools.pageEmtity(pageDTO);

        List<MallGood> mallGoodList = null;

        try {
            mallGoodList = mallGoodService.listAgentMallGood(mallGoodQueryDTO, page);
            for(int i = 0; i < mallGoodList.size(); i++) {
                MallGoodOutDTO mallGoodOutDTO = new MallGoodOutDTO();
                PropertyCopyUtil.copyProperties(mallGoodList.get(i), mallGoodOutDTO);
                mallGoodOutDTO.setCanUsedShopCount(mallGoodList.get(i).getCanUsedShop().split(",").length);
                mallGoodOutDTO.setPicture(mallGoodOutDTO.getPicture().split(",")[0]);   //只要第一张图片的url
                mallGoodOutDTOList.add(mallGoodOutDTO);
            }
            mallGoodListOutDTO.setMallGoodOutDTOList(mallGoodOutDTOList);
            mallGoodListOutDTO.setPage(page);
            mallGoodListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
        } catch (ParseException e) {
            logger.error("user parameter parse error");
            e.printStackTrace();
        } catch (Exception e) {
            mallGoodListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "服务器处理异常");
            e.printStackTrace();
        }
        return mallGoodListOutDTO;
    }

    @RequestMapping("/getShopList")
    public List<Map<String, Object>> getShopList(@RequestBody Long groupId) {
        return mallGoodService.getShopList(groupId);
    }

    /*
    * 修改商品属性（非编辑商品）
    * */
    @RequestMapping("/updateMallGood")
    public BaseOutDTO updateMallGood(@RequestBody List<MallGoodUpdateDTO> mallGoodUpdateDTOList) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        try {
            mallGoodService.updateMallGood(mallGoodUpdateDTOList);
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }

    /*
    * 删除商品
    * */
    @RequestMapping("/delMallGood")
    public BaseOutDTO delMallGood(@RequestBody List<Long> items) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        try {
            mallGoodService.delMallGood(items);
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }

    /*
    * 查询单个商品
    * */
    @RequestMapping("/getMallGood")
    public MallGoodInDTO getMallGood(@RequestBody Long id) {
        try {
            return mallGoodService.getMallGood(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
    * 商品选择
    * */
    @RequestMapping("/goodSelective")
    public MallGoodSelectiveListOutDTO goodSelective(@RequestBody MallGoodSelectiveQueryDTO mallGoodSelectiveQueryDTO) {
        try {
            return mallGoodService.goodSelective(mallGoodSelectiveQueryDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/getGoodIdByCategoryId")
    public List<Long> getGoodIdByCategoryId(@RequestBody Map<String, List<Long>> items) {
        return mallGoodService.getGoodIdByCategoryId(items);
    }

    @RequestMapping("/getGoodListById")
    public List<Map> getGoodListById(@RequestBody List<Long> goodIdList) {
        return mallGoodService.getGoodListById(goodIdList);
    }

    /*
    *获取商城商品列表
    * */
    @RequestMapping("/listEntityGood")
    public MallGoodListOutDTO listEntityGood(MallGoodQueryDTO mallGoodQueryDTO, PageDTO pageDTO) {
        try {
            Page page = Tools.pageEmtity(pageDTO);
            return mallGoodService.listEntityGood(mallGoodQueryDTO, page);
        } catch (Exception e) {
            e.printStackTrace();
            MallGoodListOutDTO mallGoodListOutDTO = new MallGoodListOutDTO();
            mallGoodListOutDTO.setErrorCode(Rescode.FAIL);
            return mallGoodListOutDTO;
        }
    }

    /**
     * 获取代理商直属门店的商品信息
     * @param mallGoodQueryDTO
     * @param pageDTO
     * @return
     */
    @RequestMapping("/shopEntityGoodList")
    public MallGoodListOutDTO getAgentShopEntityGoodList(MallGoodQueryDTO mallGoodQueryDTO, PageDTO pageDTO){
        try {
            Page page = Tools.pageEmtity(pageDTO);
            return mallGoodService.shopEntityGoodList(mallGoodQueryDTO, page);
        } catch (Exception e) {
            e.printStackTrace();
            MallGoodListOutDTO mallGoodListOutDTO = new MallGoodListOutDTO();
            mallGoodListOutDTO.setErrorCode(Rescode.FAIL);
            return mallGoodListOutDTO;
        }
    }

    /*
    *获取门店商城商品列表
    * */
    @RequestMapping("/listShopEntityGood")
    public MallGoodListOutDTO listShopEntityGood(MallGoodQueryDTO mallGoodQueryDTO, PageDTO pageDTO) {
        try {
            Page page = Tools.pageEmtity(pageDTO);
            return mallGoodService.listShopEntityGood(mallGoodQueryDTO, page);
        } catch (Exception e) {
            e.printStackTrace();
            MallGoodListOutDTO mallGoodListOutDTO = new MallGoodListOutDTO();
            mallGoodListOutDTO.setErrorCode(Rescode.FAIL);
            return mallGoodListOutDTO;
        }
    }

    /**
     * 获取本渠道下的所有门店信息
     * @param mallGoodQueryDTO
     * @return
     */
    @RequestMapping("/listShop")
    public ShopListOutDTO getShopList(MallGoodQueryDTO mallGoodQueryDTO){
        ShopListOutDTO shopListOutDTO = new ShopListOutDTO();
        ShopSelectQueryDTO shopSelectQueryDTO = new ShopSelectQueryDTO();
        shopSelectQueryDTO.setGroupId(mallGoodQueryDTO.getGroupId());
        try {
            shopListOutDTO=shopService.selectShopList(shopSelectQueryDTO);
        } catch (Exception e) {
            e.printStackTrace();
            shopListOutDTO.setErrorCode(Rescode.FAIL);
        }
        return shopListOutDTO;
    }

    /*
    *获取门店审核商城商品列表
    * */
    @RequestMapping("/listShopAuditEntityGood")
    public MallGoodListOutDTO listShopEntityGood1(MallGoodQueryDTO mallGoodQueryDTO, PageDTO pageDTO) {
        try {
            Page page = Tools.pageEmtity(pageDTO);
            return mallGoodService.listShopAuditEntityGood(mallGoodQueryDTO, page);
        } catch (Exception e) {
            e.printStackTrace();
            MallGoodListOutDTO mallGoodListOutDTO = new MallGoodListOutDTO();
            mallGoodListOutDTO.setErrorCode(Rescode.FAIL);
            return mallGoodListOutDTO;
        }
    }


    /*
    * 获取单个商城商品
    * */
    @RequestMapping(value = "/getEntityGood", method = RequestMethod.POST)
    public EntityGoodInDTO getEntityGood(@RequestBody Map<String, Long> requestInfo) {
        Long id = requestInfo.get("id");
        Long groupId = requestInfo.get("groupId");
        try {
            return mallGoodService.getEntityGood(id, groupId);
        } catch (Exception e) {
            e.printStackTrace();
            return new EntityGoodInDTO();
        }
    }

    @RequestMapping(value = "/getEntityGood", method = RequestMethod.GET)
    public EntityGoodInDTO getEntityGood1(@RequestParam Long id,@RequestParam Long groupId) {
        try {
            return mallGoodService.getEntityGood(id, groupId);
        } catch (Exception e) {
            e.printStackTrace();
            return new EntityGoodInDTO();
        }
    }

    /*
    * 创建商城商品
    * */
    @RequestMapping("/entityGoodAdd")
    public BaseOutDTO entityGoodAdd(@RequestBody EntityGoodInDTO entityGoodInDTO) {
        try {
            return mallGoodService.saveEntityGoodAdd(entityGoodInDTO);
        } catch (Exception e) {
            e.printStackTrace();
            BaseOutDTO baseOutDTO = new BaseOutDTO();
            baseOutDTO.setErrorCode(Rescode.FAIL);
            return baseOutDTO;
        }
    }

    /*
    * 修改商品属性
    * */
    @RequestMapping("/updateEntityGood")
    public BaseOutDTO updateEntityGood(@RequestBody List<MallGoodUpdateDTO> mallGoodUpdateDTOList) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        try {
            mallGoodService.updateEntityGood(mallGoodUpdateDTOList);
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }

    @RequestMapping("/delEntityGood")
    public BaseOutDTO delEntityGood(@RequestBody Map<String, Object> requestInfo) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        try {
            mallGoodService.delEntityGood((List<Long>)requestInfo.get("delItems"), (long)(Integer)requestInfo.get("groupId"));
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }
    /*
     *  会员优惠券
     * 商品选择 指定商品获取优惠券
     *
     * */
    @RequestMapping("/shareGoodSelective")
    public MallGoodSelectiveListOutDTO shareGoodSelective(@RequestBody MallGoodSelectiveQueryDTO mallGoodSelectiveQueryDTO) {
        try {
            return mallGoodService.shareGoodSelective(mallGoodSelectiveQueryDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

     /*
     * 会员优惠券
     * 商品选择 指定类目获取优惠券
     *
     * */

    @RequestMapping("/getShareGoodIdByCategoryId")
    public List<Long> getShareGoodIdByCategoryId(@RequestBody Map<String, List<Long>> items) {
        return mallGoodService.getShareGoodIdByCategoryId(items);
    }


    @RequestMapping("/checkStatusSuccess")
    public BaseOutDTO checkStatusSuccess(@RequestBody MallGoodQueryDTO mallGoodQueryDTO) {
        try {
            return mallGoodService.checkStatusSuccess(mallGoodQueryDTO);
        } catch (Exception e) {
            e.printStackTrace();
            BaseOutDTO baseOutDTO = new BaseOutDTO();
            baseOutDTO.setErrorCode(Rescode.FAIL);
            return baseOutDTO;
        }
    }
}
